package org.example.auth.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import org.example.auth.api.MemberService;
import org.example.auth.api.SmsService;
import org.example.auth.api.module.LoginMember;
import org.example.auth.service.LoginService;
import org.example.auth.vo.LoginVo;
import org.example.core.constant.RedisConstants;
import org.example.core.constant.TokenConstants;
import org.example.core.utils.JwtUtils;
import org.example.core.utils.R;
import org.example.redis.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;

/**
 * @author 小杰_cc
 * @version 1.0
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private RedisService redisService;
    @Autowired
    private MemberService memberService;
    @Autowired
    private SmsService smsService;

    public static final Long expireTime = 60000L;

    @Override
    public R<String> sendCode(String phone) {
        String  redisCode = redisService.getObject(RedisConstants.LOGIN_CODE + phone);
        if (StrUtil.isNotBlank(redisCode)){
            long codeTime = Long.parseLong(redisCode.split("_")[1]);
            if (System.currentTimeMillis() - codeTime < expireTime){
                return R.error("60秒内请勿重复获取验证码");
            }
        }
        // 生成验证码
        String code = RandomUtil.randomString("0123456789", 4);
        // 缓存验证码,5分钟过期
        redisService.setObject(RedisConstants.LOGIN_CODE + phone, code+"_"+System.currentTimeMillis(), 5L, TimeUnit.MINUTES);
        // 发送
        smsService.sendSmsCode(phone, code);
        return R.ok();
    }

    @Override
    public R<HashMap<String, String>> login(LoginVo loginVo) {
        // 获取到验证码
        String redisCode = redisService.getObject(RedisConstants.LOGIN_CODE + loginVo.getPhone());
        // 处理
        if (StrUtil.isBlank(redisCode)){
            return R.error("验证码已过期请重新获取验证码");
        }
        // 对比验证码
        String code = redisCode.split("_")[0];
        if (!code.equalsIgnoreCase(loginVo.getCode())){
            return R.error("验证码错误");
        }
        // 远程查询
        R<LoginMember> login = memberService.login(loginVo.getPhone());
        if (login.getCode() != 0){
            return R.error("登录失败请稍后再试");
        }
        LoginMember loginMember = login.getData();
        if(loginMember.getStatus() == 1){
            return R.error("抱歉账号已被禁用，请联系客服解决");
        }
        // 删除验证码
        redisService.deleteObject(RedisConstants.LOGIN_CODE + loginVo.getPhone());
        HashMap<String, Object> map = new HashMap<>();
        map.put(TokenConstants.MEMBER_ID, loginMember.getId());
        map.put(TokenConstants.MEMBER_NAME, loginMember.getUsername());
        // 返回
        HashMap<String, String> info = new HashMap<>();
        info.put("username",loginMember.getUsername());
        info.put("header",loginMember.getHeader());
        info.put("token",JwtUtils.createMemberToken(map));
        return R.ok(info);
    }
}
